package com.itheima.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.RecommendSubjectMapper;
import com.itheima.pojo.SmsHomeAdvertise;
import com.itheima.pojo.SmsHomeRecommendSubject;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;

import java.util.List;

/**
 * 首页专题
 */
@DubboService
public class SmsHomeRecommendSubjectImpl implements RecommendSubjectApi {

    @Autowired
    private RecommendSubjectMapper recommendSubjectMapper;

    /**
     * 保存数据到专题表中
     *
     * @param smsHomeRecommendSubject
     */
    @Override
    public void addSubject(SmsHomeRecommendSubject smsHomeRecommendSubject) {
        smsHomeRecommendSubject.setSort(0);
        smsHomeRecommendSubject.setRecommendStatus(1);
        recommendSubjectMapper.insert(smsHomeRecommendSubject);
    }

    /**
     * 根据ids批量删除
     *
     * @param ids
     */
    @Override
    public void deleteByIds(Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        recommendSubjectMapper.deleteBatchIds(list);
    }

    /**
     * 根据状态,专题名称 模糊查询
     *
     * @param pageNum
     * @param pageSize
     * @param recommendStatus
     * @param subjectName
     * @return
     */
    @Override
    public IPage<SmsHomeRecommendSubject> list(Integer pageNum, Integer pageSize, Integer recommendStatus, String subjectName) {
        IPage<SmsHomeRecommendSubject> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeRecommendSubject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(recommendStatus), SmsHomeRecommendSubject::getRecommendStatus, recommendStatus)
                .like(!ObjectUtils.isEmpty(subjectName), SmsHomeRecommendSubject::getSubjectName, subjectName)
                .orderByDesc(SmsHomeRecommendSubject::getSort);

        recommendSubjectMapper.selectPage(page, wrapper);

        return page;
    }

    /**
     * 修改状态
     *
     * @param recommendStatus
     * @param ids
     */
    @Override
    public void updateByIds(Integer recommendStatus, Integer[] ids) {
        SmsHomeRecommendSubject smsHomeRecommendSubject = new SmsHomeRecommendSubject();
        smsHomeRecommendSubject.setRecommendStatus(recommendStatus);
        for (Integer id : ids) {
            smsHomeRecommendSubject.setId(id.longValue());
            recommendSubjectMapper.updateById(smsHomeRecommendSubject);
        }

    }


    /**
     * 修改排序
     *
     * @param smsHomeRecommendSubject
     */
    @Override
    public void update(SmsHomeRecommendSubject smsHomeRecommendSubject) {
        recommendSubjectMapper.updateById(smsHomeRecommendSubject);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public SmsHomeRecommendSubject findByID(Integer id) {

        SmsHomeRecommendSubject smsHomeRecommendSubject = recommendSubjectMapper.selectById(id.longValue());
        return smsHomeRecommendSubject;
    }

    /**
     *
     * @param subjectId
     * @return
     */
    @Override
    public SmsHomeRecommendSubject findBySid(Integer subjectId) {
        LambdaQueryWrapper<SmsHomeRecommendSubject> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SmsHomeRecommendSubject::getSubjectId,subjectId);
        return recommendSubjectMapper.selectOne(wrapper);

    }
}
